home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 424_01 / ed_157 / rec_merge.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-10  |  2.4 KB  |  101 lines

  1. /*
  2.  * Copyright (C) 1992 by Rush Record (rhr@clio.rice.edu)
  3.  * 
  4.  * This file is part of ED.
  5.  * 
  6.  * ED is free software; you can redistribute it and/or modify it under the terms
  7.  * of the GNU General Public License as published by the Free Software Foundation.
  8.  * 
  9.  * ED is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  10.  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  11.  * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
  12.  * 
  13.  * You should have received a copy of the GNU General Public License along with ED
  14.  * (see the file COPYING).  If not, write to the Free Software Foundation, 675
  15.  * Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17. #include "opsys.h"
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22.  
  23. #include "memory.h"
  24. #include "rec.h"
  25. #include "window.h"
  26. #include "ed_dec.h"
  27.  
  28. /******************************************************************************\
  29. |Routine: rec_merge
  30. |Callby: copier killer
  31. |Purpose: Handles merging of records (deletion of "separators").
  32. |Arguments:
  33. |    prec is the record that is to be merged with its successor.
  34. \******************************************************************************/
  35. void rec_merge(prec)
  36. rec_ptr *prec;
  37. {
  38.     register rec_ptr rec,next,new,pred;
  39.     register Int l1,l2,i;
  40.  
  41.     rec = *prec;
  42.     if((next = rec->next) == BASE)
  43.     {
  44.         if(!rec->length)
  45.         {
  46.             remq(rec);
  47.             fixup_recs(rec,BASE);
  48.             if(rec == SELREC)
  49.             {
  50.                 SELREC = BASE;
  51.                 SELBYT = 0;
  52.             }
  53.             for(i = 0;i < NMARK;i++)
  54.                 if(rec == MARKREC[i])
  55.                 {
  56.                     MARKREC[i] = BASE;
  57.                     MARKBYT[i] = 0;
  58.                 }
  59.         }
  60.         return;
  61.     }
  62.     new = (rec_ptr)imalloc(sizeof(rec_node));
  63.     l1 = rec->length;
  64.     l2 = next->length;
  65.     new->data = (Char *)imalloc(l1 + l2 + 1);
  66.     new->recflags = 1;    /* it is a freeable buffer */
  67.     if(l1 > 0)
  68.         memcpy(new->data,rec->data,l1);
  69.     if(l2 > 0)
  70.         memcpy(&new->data[l1],next->data,l2);
  71.     new->length = l1 + l2;
  72.     new->data[l1 + l2] = 0;
  73.     pred = rec->prev;
  74.     remq(rec);
  75.     if(rec->data && (rec->recflags & 1))
  76.         ifree(rec->data);
  77.     ifree(rec);
  78.     remq(next);
  79.     if(next->data && (next->recflags & 1))
  80.         ifree(next->data);
  81.     ifree(next);
  82.     fixup_recs(rec,new);
  83.     fixup_recs(next,new);
  84.     if(rec == SELREC || next == SELREC)
  85.     {
  86.         if(next == SELREC)
  87.             SELBYT += l1;
  88.         SELREC = new;
  89.     }
  90.     for(i = 0;i < NMARK;i++)
  91.         if(rec == MARKREC[i] || next == MARKREC[i])
  92.         {
  93.             if(next == MARKREC[i])
  94.                 MARKBYT[i] += l1;
  95.             MARKREC[i] = new;
  96.         }
  97.     insq(new,pred);
  98.     *prec = new;
  99. }
  100.  
  101.